<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\WorkOrderComponent;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
use Dcat\Admin\Http\Controllers\AdminController;

class WorkOrderComponentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WorkOrderComponent(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('work_order_id');
            $grid->column('type');
            $grid->column('title');
            $grid->column('position_type');
            $grid->column('standard_quantity_base');
            $grid->column('performance_quantity_base');
            $grid->column('standard_working_base');
            $grid->column('performance_working_base');
            $grid->column('damage_coefficient_type');
            $grid->column('damage_coefficient');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WorkOrderComponent(), function (Show $show) {
            $show->field('id');
            $show->field('work_order_id');
            $show->field('type');
            $show->field('title');
            $show->field('position_type');
            $show->field('standard_quantity_base');
            $show->field('performance_quantity_base');
            $show->field('standard_working_base');
            $show->field('performance_working_base');
            $show->field('damage_coefficient_type');
            $show->field('damage_coefficient');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WorkOrderComponent(), function (Form $form) {
            $form->display('id');
            $form->text('work_order_id');
            $form->text('type');
            $form->text('title');
            $form->text('position_type');
            $form->text('standard_quantity_base');
            $form->text('performance_quantity_base');
            $form->text('standard_working_base');
            $form->text('performance_working_base');
            $form->text('damage_coefficient_type');
            $form->text('damage_coefficient');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function orderComponentDelete(Request $request)
    {
        $param = $request->all();

        \App\Models\WorkOrderComponent::where('work_order_id', $param['work_order_id'])
            ->where('component_maintenance_id', $param['component_maintenance_id'])
            ->delete();

        return successMessage();
    }
}
